package com.servlet.dao;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import DB.DBManager;

import com.servlet.vo.ConsumptionType;

public class ConsumptionTypeDao {

	private Connection con;
	private Statement statement;
	private ResultSet resultSet;
	private boolean flag = false;
	private StringBuffer sql = new StringBuffer();
	
	public ConsumptionTypeDao() throws SQLException{
		con = DBManager.getConn();
		statement = con.createStatement();
	}
	public void close(){
		
		if(null != resultSet){
			try {
				resultSet.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(null != statement){
			try {
				statement.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		if(null != con){
			try {
				con.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public boolean save(ConsumptionType obj) {
		
		sql.append("INSERT INTO ConsumptionType(ADDTIME,deleteStatus,consumptionTypeName) VALUES( NOW(),FALSE,'");
		sql.append(obj.getConsumptionTypeName() + "')");
		
		try {
			flag = statement.execute(sql.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			flag = true;
		}finally{
			this.close();
		}
		return flag;
	}
	
	public boolean update(ConsumptionType obj){
		
		try {
			sql.append("UPDATE ConsumptionType(");
			
			if(obj.getConsumptionTypeName() != null){
				sql.append("consumptionTypeName,");
			}
			
			if(sql.toString().endsWith(",")){
				sql.deleteCharAt(sql.toString().length()-1);
				sql.append(") values(");
				
				if(obj.getConsumptionTypeName() != null){
					sql.append("'" + obj.getConsumptionTypeName() + "',");
				}
				
				if(sql.toString().endsWith(",")){
					sql.deleteCharAt(sql.toString().length()-1);
					sql.append(")");
					
					try {
						flag = statement.execute(sql.toString());
					} catch (SQLException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		}finally{
			this.close();
		}
		return flag;
	}
	
	public boolean delete(Long id){
		
		sql.append("UPDATE ConsumptionType SET deleteStatus = TRUE WHERE id = '" + id + "'");
		try {
			flag = statement.execute(sql.toString());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.close();
		}
		return flag;
	}
	
	public List<ConsumptionType> getList(){
		
		sql.append("SELECT id,consumptionTypeName FROM ConsumptionType WHERE deleteStatus = FALSE ");
		List<ConsumptionType> objs = new ArrayList<ConsumptionType>();
		try {
			resultSet = statement.executeQuery(sql.toString());
			ConsumptionType obj = null;
			while(resultSet.next()){
			
				obj = new ConsumptionType();
				obj.setId(resultSet.getLong("id"));
				obj.setConsumptionTypeName(resultSet.getString("consumptionTypeName"));
				objs.add(obj);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}finally{
			this.close();
		}
		return objs;
	}
}
